<?php
class Admin_Model_Admin extends Core_Database
{
    /**
     * Kiem tra dang nhap
     * 
     * @param : username password
     * @return: array(admin_id,fullname) : neu ton tai    null : neu khong ton tai
     * */
   public function login($username,$password)
   {
     $sql = 'select admin_id, fullname from admin where username = :username and password = PASSWORD(:password)';
     $sth = $this->_db->prepare($sql);
     $sth->bindParam(':username',$username,PDO::PARAM_STR);
     $sth->bindParam(':password',$password,PDO::PARAM_STR);
     $status = $sth->execute();
     if (!$status)
          return null;
     $result = $sth->fetchAll();
     if(count($result)>0)
        return $result;
     else
        return null;     
   }
   
   /**
    * Lay thong tin cua mot admin
    * 
    * @param : admin_id
    * @return : array : neu ton tai  null : neu khong ton tai
    * 
    * */
    public function getAdmin($admin_id)
    {
     $sql = 'select * from admin where admin_id = :admin_id';
     $sth = $this->_db->prepare($sql);
     $sth->bindParam(':admin_id',$admin_id,PDO::PARAM_INT);
     $status = $sth->execute();
     if (!$status)
          return null;
     $result = $sth->fetchAll();
     if(count($result)>0)
        return $result;
     else
        return null;     
    }
    /**
     * Doi thong tin ho ten cua admin
     * @param : admin_id  fullname
     * @return : true : thanh cong , false: that bai
     * */
     public function updateFullname($admin_id,$fullname)
     {
        $sql = 'update admin set fullname = :fullname where admin_id = :admin_id';
    	$sth = $this->_db->prepare($sql);
    	$sth->bindParam(':admin_id',$admin_id,PDO::PARAM_INT);
  	    $sth->bindParam(':fullname', $fullname, PDO::PARAM_STR);
    	return $sth->execute();
     }
     /**
      * Doi mat khau
      * @param : admin_id old_password password
      * @return: 0: mat khau cu sai, 1: that bai, 2: thanh cong
      * 
      * */
      public function changePassword($admin_id,$old_password,$password)
      {
        $sql = 'select admin_id from admin where admin_id = :admin_id and password = PASSWORD(:old_password)';
        $sth = $this->_db->prepare($sql);
    	$sth->bindParam(':admin_id',$admin_id,PDO::PARAM_INT);
  	    $sth->bindParam(':old_password', $old_password, PDO::PARAM_STR);
    	if(! $sth->execute())
            return 1;
        if(count($sth->fetchAll())==0)
            return 0;
            
        $sql = 'update admin set password = PASSWORD(:password) where admin_id = :admin_id';
        $sth = $this->_db->prepare($sql);
        $sth->bindParam(':admin_id',$admin_id,PDO::PARAM_INT);
  	    $sth->bindParam(':password', $password, PDO::PARAM_STR);
    	if(! $sth->execute())
            return 1;
        else
            return 2;
      }
        
}